home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ABUSESRC.ZIP / AbuseSrc / abuse / src / net / inc / ipx.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-26  |  12.6 KB  |  372 lines

  1. #ifndef __IPX_SOCK_HPP_
  2. #define __IPX_SOCK_HPP_
  3.  
  4. #include "sock.hpp"
  5. #include "macs.hpp"
  6. #include "timing.hpp"
  7. #include <string.h>
  8.  
  9. #include <string.h>
  10.  
  11. class ipx_net_address : public net_address
  12.   public :
  13.   uchar addr[10];
  14.   ushort port;      // stored in intel-format
  15.   virtual protocol protocol_type() const { return IPX; }
  16.   virtual int equal(const net_address *who)  const
  17.   { return who->protocol_type()==protocol_type() && 
  18.       memcmp(addr,((ipx_net_address *)who)->addr,10)==0; }
  19.  
  20.   virtual int set_port(int Port);
  21.   virtual void print() { printf("network = %x.%x.%x.%x, node = %x.%x.%x.%x.%x.%x, port = %d\n",
  22.                          addr[0],addr[1],addr[2],addr[3],addr[4],addr[5],addr[6],addr[7],addr[8],addr[9],port); }  
  23.   ipx_net_address() { memset(addr,0,10); port=0; }
  24.   ipx_net_address(uchar *addr_data, ushort port) : port(port) 
  25.   { memcpy(addr,addr_data,10); }
  26.   int get_port() { return port ; }
  27.   net_address *copy() { return new ipx_net_address(addr,port); }
  28.   void store_string(char *st, int st_length)
  29.   {
  30.     char buf[100];
  31.     sprintf(buf,"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x:%d",addr[0],addr[1],addr[2],addr[3],addr[4],addr[5],addr[6],addr[7],addr[8],addr[9],port);
  32.     strncpy(st,buf,st_length);
  33.     st[st_length-1]=0;
  34.   }
  35. } ;
  36.  
  37. #pragma pack (1)
  38.  
  39.  
  40. class ipx_net_socket : public net_socket
  41. {
  42.   public :
  43.   uchar *backup;                   // when a partial read request happens
  44.   int backup_start,backup_end,backup_size;
  45.   int read_backup(void *&buf, int &size);
  46.   void add_backup(uchar *buf, int size);
  47.  
  48.   ipx_net_socket *next;
  49.   int total_packets;
  50.  
  51.   int ipx_packet_total() { return total_packets; }
  52.  
  53.   enum {IPX_PACKET_SIZE=1024} ;      // this amount of data space reserved for each packet
  54.                                     // though the actaul packet sent may be smaller because of IPX limits
  55.  
  56.  
  57.   enum { ACK=1,            // acknowedge receipt of packet
  58.      FIN=2,            // connection has finished
  59.      CONNECT=4,        // asking to make a connection
  60.      WRITE_DATA=8,
  61.      WRITE_CONFIRM=16,
  62.      ALL_FLAGS=31 };
  63.  
  64.   enum { SEND_RETRY_TIMEOUT=10,  // .1 seconds timeout before resending data packet
  65.      RETRY_TOTAL=2400        // give up after retring 2400 times, or 240 seconds =  4 minutes
  66.        } ;
  67.  
  68.  
  69.  
  70.   struct ipx_packet
  71.   {
  72.     struct ECBStructure
  73.     {
  74.       ushort Link[2];
  75.       ushort ESRAddress[2];
  76.       uchar  InUseFlag;
  77.       uchar  CompletionCode;
  78.       ushort ECBSocket;
  79.       uchar  IPXWorkspace[4];
  80.       uchar  DriverWorkspace[12]; 
  81.       uchar  ImmediateAddress[6]; 
  82.       ushort FragmentCount;
  83.       ushort fAddress[2];
  84.       ushort fSize;
  85.  
  86.       int data_waiting() { return InUseFlag; }
  87.       void set_data_size(int size);
  88.     } ecb;
  89.  
  90.     struct IPXPacketStructure
  91.     {
  92.       ushort PacketCheckSum;
  93.       ushort PacketLength;
  94.       uchar  PacketTransportControl;
  95.       uchar  PacketType;
  96.  
  97.       uchar  dAddr[10];
  98.       ushort dSocket;      // flipped intel-format
  99.  
  100.       uchar  sAddr[10];
  101.       ushort sSocket;     // flipped intel-format
  102.  
  103.     } ipx;
  104.  
  105.  
  106.     uchar data[IPX_PACKET_SIZE];
  107.  
  108.     int packet_prefix_size()                 { return 6; }    // 2 size
  109.                                                               // 2 byte check sum
  110.                                                               // 1 byte packet order
  111.                                                               // 1 byte flags
  112.        
  113.  
  114.     void set_packet_size(unsigned short x)   { *((unsigned short *)data)=lstl(x); }
  115.     unsigned short packet_size()             { unsigned short size=(*(unsigned short *)data); return lstl(size); }
  116.     unsigned char tick_received()            { return data[4]; }  
  117.     void set_tick_received(unsigned char x)  { data[4]=x; }
  118.     unsigned char *packet_data()             { return data+packet_prefix_size(); }
  119.     unsigned short get_checksum()            { unsigned short cs=*((unsigned short *)data+1); return lstl(cs); }
  120.     uchar get_flag(int flag)                 { return data[5]&flag; }
  121.     void set_flag(int flag, int x)           { if (x) data[5]|=flag; else data[5]&=~flag; }
  122.     unsigned short calc_checksum()
  123.     {
  124.       *((unsigned short *)data+1)=0;
  125.       int i,size=packet_prefix_size()+packet_size();
  126.       unsigned char c1=0x12,c2=0x34,*p=data;
  127.       for (i=0;i<size;i++,p++)
  128.       {
  129.     c1+=*p;
  130.     c2+=c1;
  131.       }
  132.       unsigned short cs=( (((unsigned short)c1)<<8) | c2);
  133.       *((unsigned short *)data+1)=lstl(cs);
  134.       return cs;
  135.     }
  136.  
  137.  
  138.     void reset()    { set_packet_size(0); set_flag(ALL_FLAGS,0); }     // 2 bytes for size, 1 byte for tick
  139.     
  140.     void add_to_packet(void *buf, int size);
  141.  
  142.     void write_byte(unsigned char x) { add_to_packet(&x,1); }
  143.     void write_short(unsigned short x) { x=lstl(x); add_to_packet(&x,2); }
  144.     void write_long(unsigned long x) { x=lltl(x); add_to_packet(&x,4); }
  145.  
  146.  
  147.   } *pk;
  148.  
  149.      
  150.  
  151.   int fd,er;
  152.   void idle();   // call this if you are bored
  153.   
  154.   enum { INIT=0,
  155.          READ_CHECKING=1,
  156.      WRITE_CHECKING=2,
  157.      READ_SELECTED=4,
  158.      WRITE_SELECTED=8 
  159.        } ;
  160.   uchar status;
  161.  
  162.   void send_data(int data_size);
  163.   void send_ping_packet();
  164.   int listen_to_packet(int packet_num);
  165.   int open_socket(int port);
  166.  
  167.   virtual int error()                                              { return er; }
  168.   virtual int ready_to_read();
  169.   virtual int ready_to_write() { if (fd>=0) return pk[0].ecb.InUseFlag; else return 1; }
  170.  
  171.   virtual int write(void *buf, int size, net_address *addr=NULL) { return 0; }
  172.  
  173.   virtual int read(void *buf, int size, net_address **addr=0) { if (addr) *addr=0; return 0; }
  174.   virtual int get_fd()    { return fd; }
  175.   virtual void read_selectable()     { status|=READ_CHECKING; }
  176.   virtual void read_unselectable()   { status&=~READ_CHECKING; }
  177.   virtual void write_selectable()    { status|=WRITE_CHECKING; }
  178.   virtual void write_unselectable()  { status&=~WRITE_CHECKING; }
  179.   virtual int listen(int port)       { return 0; }
  180.   virtual net_socket *accept(net_address *&from) { from=NULL; return NULL; }
  181.   virtual int from_port() { return -1; }
  182.  
  183.   virtual void clear();
  184.  
  185.   void set_socket_dest(uchar *addr, ushort port);
  186.   ipx_net_socket(int port, int total_read_packets, ipx_net_address *dest);
  187.   virtual void print() { fprintf(stderr,"ipx net socket");  }
  188.   virtual ~ipx_net_socket();
  189. } ;
  190.  
  191. #pragma pack (0)
  192.  
  193.  
  194. class ipx_secure_listening_socket;
  195.  
  196. class ipx_secure_socket : public ipx_net_socket
  197. {  
  198.   uchar packet_on,write_ack_needed,write_ack_checksum;
  199.   ushort parent_port;
  200.  
  201.   enum { ESTABLISHED, 
  202.      CLOSE_WAIT,        // waiting for user to close
  203.      CLOSING,           // waiting for remote response to close
  204.      TIME_WAIT,         // waiting till 'counter' expires to ensure remote socket has gotten close message
  205.        } state;
  206.  
  207.   int counter,closed;
  208.   
  209.   public :
  210.   enum { SECURE_TOTAL_READ_PACKETS=2 } ;
  211.  
  212.   virtual int ready_to_read();
  213.   virtual int ready_to_write() { return 1; }
  214.  
  215.   virtual int write(void *buf, int size, net_address *addr=NULL);
  216.   virtual int read(void *buf, int size, net_address **addr=0);
  217.  
  218.   void clear_packet(int i);
  219.   ipx_secure_socket(int parent_port, int port, ipx_net_address *dest) : 
  220.     ipx_net_socket(port,SECURE_TOTAL_READ_PACKETS,dest),parent_port(parent_port)
  221.   { packet_on=1; write_ack_needed=0; closed=0; }
  222.   int from_port() { return parent_port; }
  223.   int verify_sync();
  224.   virtual void print() { fprintf(stderr,"ipx secure socket");  }
  225.   ~ipx_secure_socket();
  226. } ;
  227.  
  228.  
  229. // the closed_secure_ipx_packet is not to be seen by anyone other than the ipx_protocol
  230. // and it's is polled during calls to select()
  231.  
  232. class closed_secure_ipx_socket
  233. {
  234.   public :
  235.   closed_secure_ipx_socket *next;
  236.  
  237.   enum { CLOSING,                       // keep send close message, until receive ack, then goto TIME_WAIT
  238.      TIME_WAIT                      // after ack for close message, wait for TIME_WAIT_EXPIRE before closing
  239.        } state;
  240.  
  241.   enum { CLOSE_MSG_RETRY_TIMEOUT=20,    // .2 seconds between each close() message retry
  242.          CLOSE_TOTAL_RETRY=10,          // 10 total retries for sending a close() message before giving up
  243.      TIME_WAIT_EXPIRE=1000          // 10 seconds before a socket expires and can be reused
  244.        } ;
  245.  
  246.   time_marker start_counter;      // if CLOSING, then the clock() of the last close message sent
  247.                                   // if TIME_WAIT, then clock() of the entering this state
  248.  
  249.   int close_send_count;           // number of times close socket msg has been sent (from CLOSING state)
  250.                                   
  251.   int fd;                          // the port this socket is using
  252.   uchar final_packet_number;
  253.   ipx_net_socket::ipx_packet *pk;  // pointer to low packet memory taken from a ipx_secure_socket
  254.   int poll();                      // called by select, return 0 when socket is expired
  255.   ~closed_secure_ipx_socket();
  256.   int tp;
  257.   closed_secure_ipx_socket(ipx_net_socket::ipx_packet *pk, 
  258.                int total_packets,
  259.                int fd, 
  260.                unsigned char final_packet_number, 
  261.                closed_secure_ipx_socket *next);
  262.  
  263.   void print() { fprintf(stderr,"closed secure socket, state=%d",(int)state);  }
  264. } ;
  265.  
  266.  
  267. // this class can only sit and listen, it will respond to 'ping' and 'connect' packets
  268. // this class will return 1 for ready_to_read and then create a ipx_secure_socket on accept()
  269.  
  270. class ipx_secure_listening_socket : public ipx_net_socket
  271. {
  272.   enum { NAME_MAX=30 };
  273.   char name[NAME_MAX];
  274.   public :
  275.  
  276.   virtual int ready_to_write() { return 1; }
  277.   virtual int ready_to_read();
  278.   virtual int write(void *buf, int size, net_address *addr=NULL) { return 0; }
  279.   virtual int read(void *buf, int size, net_address **addr=0) { if (addr) *addr=0; return 0; }
  280.  
  281.   virtual net_socket *accept(net_address *&from);
  282.   ipx_secure_listening_socket(int port, ipx_net_address *dest, char *sock_name) : ipx_net_socket(port,2,dest) 
  283.   { strncpy(name,sock_name,NAME_MAX-1); name[NAME_MAX-1]=0; }
  284.   ~ipx_secure_listening_socket();
  285.   virtual void print() { fprintf(stderr,"ipx secure listening socket");  }
  286. } ;
  287.  
  288. class ipx_fast_socket : public ipx_net_socket
  289. {
  290.   public :
  291.   int write(void *buf, int size, net_address *addr=NULL);
  292.   int read(void *buf, int size, net_address **addr=0);
  293.   virtual int ready_to_read();
  294.   ipx_fast_socket(int port, ipx_net_address *dest) : ipx_net_socket(port,16,dest) { ; }
  295.   void clear() { ; }  // no need to clear these
  296.   virtual void print() { fprintf(stderr,"ipx fast socket");  }
  297.  
  298. } ;
  299.  
  300. class ipx_net_protocol : public net_protocol
  301. {
  302.   int Installed;
  303.   ushort max_pksz;
  304.   int max_packet_size() { return max_pksz; }
  305.  
  306.   net_address *find_host(int port, int timeout);
  307.   ipx_net_socket *list;
  308.  
  309.   class locator_node
  310.   {
  311.     public :
  312.     ipx_net_address *addr;
  313.     locator_node *next;
  314.     locator_node(ipx_net_address *addr, locator_node *next) : addr(addr), next(next) { ; }
  315.     ~locator_node() { delete addr; }
  316.   } *find_list;
  317.  
  318.   ipx_net_socket *locator_socket;
  319.   time_marker last_ping;
  320.   enum { LOCATOR_PACKETS=16 } ;
  321.  
  322.   public :
  323.   void *low_paragraph;
  324.  
  325.   enum { CONNECT_PING_RETRY=10,     // retry connetc command every .1 seconds
  326.      CONNECT_RETRY_TOTAL=2400  // retry for 4 mins.
  327.        } ;
  328.      
  329.   closed_secure_ipx_socket *closed_list;
  330.  
  331.   uchar local_addr[10];
  332.  
  333.   virtual net_address *get_local_address();
  334.   virtual net_address *get_node_address(char *&server_name, int def_port, int force_port);
  335.   virtual net_socket *connect_to_server(net_address *addr, 
  336.                     net_socket::socket_type sock_type=net_socket::SOCKET_SECURE
  337.                     );  
  338.   virtual net_socket *create_listen_socket(int port, net_socket::socket_type sock_type, char *sock_name);
  339.  
  340.   int select(int block);
  341.  
  342.   int installed();
  343.  
  344.   char *name()    { return "IPX driver"; }
  345.   void add_socket_to_list(ipx_net_socket *who);
  346.   void remove_socket_from_list(ipx_net_socket *who);
  347.  
  348.   int max_packet_data_size() { int x=max_pksz-sizeof(ipx_net_socket::ipx_packet::IPXPacketStructure);  // max IPX will allow
  349.                    if (x>ipx_net_socket::IPX_PACKET_SIZE)                      // max space we have
  350.                       return ipx_net_socket::IPX_PACKET_SIZE; else return x; 
  351.                  }
  352.   void clear_sockets(ipx_net_socket *exclude);
  353.   ipx_net_socket *socket_origination(uchar *addr, ushort other_port);     // finds a socket connected to address, started from port 'port'
  354.   void add_closed(ipx_net_socket::ipx_packet *pk, int total_packets, int fd, int packet_on);
  355.   void cleanup();
  356.   int free_closed_socket();       // returns 1 if able to close 1 closed socket in FIN state
  357.   int socket_can_be_allocated(int num_packets);
  358.   ipx_net_protocol();
  359.   void show_socks();
  360.   ~ipx_net_protocol();
  361.  
  362.   net_address *find_address(int port, char *name);
  363.   void reset_find_list();
  364. } ;
  365.  
  366. extern ipx_net_protocol ipx_net;
  367.  
  368.  
  369. #endif
  370.  
  371.